<template>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>文件管理</el-breadcrumb-item>
    <el-breadcrumb-item>我的文件</el-breadcrumb-item>
  </el-breadcrumb>

  <!-- 内容区域 -->
  <el-card class="box-card">
    <!-- 搜索条件 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入文件名" v-model="queryParams.fileName">
          <template #append>
            <el-button icon="Search" @click="search(1)"/>
          </template>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="danger" @click="search">删除</el-button>
      </el-col>
    </el-row>

    <el-table :data="fileList" stripe>
      <el-table-column type="selection" width="100"></el-table-column>
      <el-table-column prop="fileId" label="文件id" width="100"></el-table-column>
      <el-table-column prop="fileName" label="文件名" width="150"></el-table-column>
      <el-table-column prop="fileSize" label="文件大小"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="fileStatus" label="文件状态" width="200">
        <template v-slot="scope">
          <el-button type="primary" icon="el-icon-edit" size="small" @click="updateFile(scope.row.fileId)">修改
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteFile(scope.row.fileId)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="total"
                   :current-page="queryParams.pageNum" :page-sizes="[1, 5, 10]" :page-size="queryParams.pageSize"/>
  </el-card>
</template>

<script setup>
import {inject, onMounted, ref} from 'vue';
import axios from '@admin/utils/request.js';
// 注入全局属性
let $message = inject('$message');
let $confirm = inject('$confirm');

// 查询参数
const queryParams = ref({
  fileId: '',
  fileName: 'a.txt',
  parentId: ''
});

// 查询结果
let fileList = ref([]);
const total = ref(0);

// 获取文件列表
const search = function () {
  axios.get('/file/search', {
    params: {
      fileName: queryParams.value.fileName,
      parentId: queryParams.value.parentId
    }
  }).then(response => {
    console.log(response.data)
    fileList.value = response.data;
  })
};

// 删除文件
const deleteFile = function (fileId) {
  $confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    console.log(fileId)
    axios.delete('/file/delete', {
      params: {fileId: fileId}
    })
  })
};

// 修改文件
const updateFile = function (fileId) {
  // 点击修改弹框修改文件名
  console.log(fileId)
  axios.put('/file/rename', {}, {
    params: {fileId: fileId, newName: "XJ"}
  })
};

// 组件挂载时调用 search 方法
onMounted(() => {
  search();
});

</script>

<style scoped>
.box-card {
  margin-top: 20px;
}

.image-preview {
  margin-top: ¼em;
}
</style>

<style scoped>
.box-card {
  margin-top: 20px;
}
</style>